package com.usiful.manager;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.io.FileUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.usiful.model.Product;
import com.usiful.schema.UsifulConfigSchema;
import com.usiful.util.UsifulEmailHelper;
import com.usiful.util.UsifulExcelHelper;
import com.usiful.util.UsifulGsonHelper;
import com.usiful.util.UsifulHtmlHelper;

/**
 * 
 * @author [周织能]
 * @description 
 * @date 2016年8月1日
 * @email 742408364@qq.com
 */
public class CrawlerMananger {

	private static CrawlerMananger instance;
	protected ExecutorService executorService;
	protected final int max_thread_num = 5;
	protected List<Product> wait4ServingProducts;

	private CrawlerMananger() {
		executorService = Executors.newFixedThreadPool(max_thread_num);
	}

	public static CrawlerMananger getInstance() {
		if (instance == null) {
			instance = new CrawlerMananger();
		}
		return instance;
	}

	public void start() {
		executorService.submit(new Runnable() {

			public void run() {
				// TODO Auto-generated method stub
				List<Product> products = UsifulHtmlHelper.getInstance().parseUsifulProductInfo();
				File compareFile = new File(UsifulConfigSchema.COMPARE_FILE);
				List<Product> originalProducts = null;
				List<Product> resultProducts = new ArrayList<Product>();
				if (compareFile.exists()) {
					try {
						String originalProductStr = FileUtils.readFileToString(compareFile, "UTF-8");
						originalProducts = new Gson().fromJson(originalProductStr, new TypeToken<List<Product>>() {
						}.getType());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (originalProducts == null) {
					resultProducts.addAll(products);
				} else {
					HashMap<String, Product> originalProductMap = new HashMap<String, Product>();
					for (int i = 0; i < originalProducts.size(); i++) {
						originalProductMap.put(originalProducts.get(i).getHomeUrl(), originalProducts.get(i));
					}
					for (int j = 0; j < products.size(); j++) {
						if(!originalProductMap.containsKey(products.get(j).getHomeUrl())) {
							resultProducts.add(products.get(j));
						}
					}
				}
				
				if(resultProducts != null && resultProducts.size() > 0) {
					if(originalProducts == null) {
						originalProducts = new ArrayList<Product>();
					}
					originalProducts.addAll(resultProducts);
				}

				try {
					if(originalProducts != null && originalProducts.size() > 0) {
						FileUtils.forceDelete(compareFile);
						String originalStr = UsifulGsonHelper.object2Json(originalProducts);
						FileUtils.writeStringToFile(new File(UsifulConfigSchema.COMPARE_FILE),originalStr);
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(resultProducts != null && resultProducts.size() > 0) {
					UsifulExcelHelper.getInstance().createFollowSaleExcel(UsifulConfigSchema.RESULT_FILE,resultProducts);
					UsifulEmailHelper.send2Yami();
				}
			}});
		
	}

}
